
import os, _pg
import project, cscope

def createTables(db):
    q = """
CREATE TABLE calls(
	CallID		serial		NOT NULL,
	CallerFuncID	int		references Functions(FuncID) NOT NULL,
	TargetFuncID	int		references Functions(FuncID) NOT NULL,
	--Caller		varchar		NOT NULL,
	--Target		varchar		NOT NULL,
	date		date		NOT NULL,
PRIMARY KEY (CallID)
);
"""
    dropq = "drop table calls cascade;"

    try:
        print "cleaning Calls table..."
        db.query(dropq)
        print "...OK!"
    except _pg.ProgrammingError:
            print "could not drop tables"
    try:
        print "creating Callss table..."
        db.query(q)
        print "...OK!"
    except _pg.ProgrammingError, e:
        print "could not create tables"
        raise project.ProjectError("Could not create tables, cause:" + str(e))



def start(proj, log):
    db = proj.getDatabase()
    repo = proj.getSVNRepository()
    createTables(db)
    cscope.calcMonthlyCallGraphs(db, repo, log)
    
